package com.example.userlogon.service;

import com.example.userlogon.dao.UserDao;
import com.example.userlogon.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements UserDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int add(User user) {
        String sql="insert into sys_user values(null,'"+user.getName()
                +"','"+user.getPassword()+"','0','0')";
        System.out.println(sql);
        return jdbcTemplate.update(sql);
    }

    @Override
    public int delete(User user) {
        return 0;
    }

    @Override
    public int update(User user) {
        return 0;
    }

    @Override
    public User findOne(User user) {
        String sql = "select * from sys_user where name like '%"+user.getName()+"%'";
        System.out.println(sql);
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class));
    }

    @Override
    public List<User> findAll() {
        String sql="select * from sys_user";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));

    }
}
